package com.wangtianxiang.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.wangtianxiang.rabbitmq.config.CusRabbitProperties;
import com.wangtianxiang.rabbitmq.config.FlushRabbitProperties;
import com.wangtianxiang.rabbitmq.config.RabbitMqConfig;
import com.wangtianxiang.rabbitmq.constants.RabbitMqConfigConstants;
import com.wangtianxiang.rabbitmq.domain.Order;
import com.wangtianxiang.rabbitmq.exception.DynamicRabbitNotFountException;
import com.wangtianxiang.rabbitmq.factory.RabbitTemplateFactory;
import com.wangtianxiang.rabbitmq.utils.SpringBeanUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
@SpringBootApplication
public class BootApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class,args);
    }

//    @Autowired
//    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitTemplateFactory rabbitTemplateFactory;


    @RequestMapping(value = "test")
    public String test(String tenant) throws DynamicRabbitNotFountException {
        Date date = new Date();
        Order order = new Order("13232312","广东省",date,date);
        RabbitTemplate rabbitTemplate = rabbitTemplateFactory.getRabbitTemplate(tenant);
        rabbitTemplate.convertAndSend("topic.sms.email",JSON.toJSONString(order));
        return "test";
    }

    @RequestMapping(value = "flush")
    public String flush(String tenant, @RequestBody FlushRabbitProperties properties){
        rabbitTemplateFactory.flushRabbitTemplate(tenant,properties);
        return "flush success";
    }


    @RequestMapping(value = "isSingleton")
    public boolean isSingleton(String tenant){
        return SpringBeanUtils.isSingleton(tenant + RabbitMqConfigConstants.BEAN_TEMPLATE_SUFFIX);
    }


 /*   @RequestMapping(value = "test2")
    public String test2(){
        Date date = new Date();
        Order order = new Order("13232312","广东省",date,date);
        rabbitTemplate.convertAndSend("1234","topic.sms.email",JSON.toJSONString(order));
        return "test";
    }

    @RequestMapping(value = "test3")
    public String test3(){
        Date date = new Date();
        Order order = new Order("13232312","广东省",date,date);
        rabbitTemplate.convertAndSend("123","topic.sms.email",JSON.toJSONString(order));
        return "test";
    }


    @RequestMapping(value = "user")
    public String user(){
        User user = new User("狗蛋",15);
        rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NAME,"topic.sms.email",user);
        return "user";
    }*/
}
